package cc.rengu.igas.bsps.core.service.trans;

import cc.rengu.igas.bsps.common.dao.BspsMchntBindInfoMapper;
import cc.rengu.igas.bsps.common.dao.BspsTermInfoMapper;
import cc.rengu.igas.bsps.common.dao.impl.BspsMchntBindInfoMapperImpl;
import cc.rengu.igas.bsps.common.dao.impl.BspsTermInfoMapperImpl;
import cc.rengu.igas.bsps.common.entity.MchntBindInfo;
import cc.rengu.igas.bsps.common.entity.TermInfo;
import cc.rengu.igas.bsps.common.enums.RespCodeEnum;
import cc.rengu.igas.bsps.core.service.base.ManageService;
import cc.rengu.igas.bsps.facade.request.MchntTermEditRequest;
import cc.rengu.igas.bsps.facade.response.MchntTermEditResponse;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.StringUtil;

public class MchntTermEditService extends ManageService {

    protected Object bizBeforeCust() throws Exception {
        MchntTermEditRequest MchntTermEditRequest = new MchntTermEditRequest();
        ConvertUtil.convertOutput(MchntTermEditRequest);
        return MchntTermEditRequest;
    }

    protected BizResponse bizProcessCust(Object request) throws Exception {
        MchntTermEditRequest requestParams = (MchntTermEditRequest) request;

        String instId = requestParams.getHeader().getInstId();
        String userId = requestParams.getUserId();
        String mchntNo = requestParams.getMchntNo();//商户号
        String termNo = requestParams.getTermNo();//终端编号
        String contactName = requestParams.getContactName();//终端联系人
        String contactPhone = requestParams.getContactPhone();//终端联系电话

        if (StringUtil.isEmptyOrNull(mchntNo) || StringUtil.isEmptyOrNull(instId) || StringUtil.isEmptyOrNull(termNo)) {
            rglog.error("必填性校验失败!");
            throw new BizException(RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespCode(), RespCodeEnum.MSG_VALID_CHECK_ERROR.getRespDesc());
        }
        //商户绑定用户关系
        BspsMchntBindInfoMapper mchntBindInfoMapper = new BspsMchntBindInfoMapperImpl();
        MchntBindInfo mchntBindInfo = mchntBindInfoMapper.selectBspsMchntBindInfoListByUserIdAndMchntNo(instId, userId, mchntNo);
        if (mchntBindInfo == null) {
            rglog.error("商户绑定关系不存在");
            throw new BizException(RespCodeEnum.GET_USER_BIND_INFO_ERROR.getRespCode(), RespCodeEnum.GET_USER_BIND_INFO_ERROR.getRespDesc());
        }
        //根据主键查询终端信息
        BspsTermInfoMapper mchntTermMapper = new BspsTermInfoMapperImpl();
        TermInfo termInfo = mchntTermMapper.selectBspsTermInfoByPrimaryKey(instId, mchntNo, termNo);
        if (null == termInfo) {
            rglog.error("查询终端信息失败!法人行:<{}>，终端编号:<{}>", instId, termNo);
            throw new BizException(RespCodeEnum.QUERY_MCHNT_TERM_INFO_ERROR.getRespCode(), RespCodeEnum.QUERY_MCHNT_TERM_INFO_ERROR.getRespDesc());//?
        }

        int iReturnCode = mchntTermMapper.updateTermContactNameContactPhoneByPrimaryKey(instId, mchntNo, termNo, contactName, contactPhone);
        if (0 != iReturnCode) {
            rglog.error("更新终端信息失败,iReturnCode:<{}>", iReturnCode);
            throw new BizException(RespCodeEnum.UPDATE_MCHNT_TERM_INFO_ERROR.getRespCode(), RespCodeEnum.UPDATE_MCHNT_TERM_INFO_ERROR.getRespDesc());
        }

        /* 拼装应答报文 */
        MchntTermEditResponse mchntTermEditResponse = new MchntTermEditResponse();
        BizResponse bizResponse = new BizResponse();
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(mchntTermEditResponse);
        return bizResponse;

    }

    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        MchntTermEditResponse mchntTermEditResponse = (MchntTermEditResponse) bizResponse.getResult();
        ConvertUtil.convertInput(mchntTermEditResponse);
    }
}
